package com.huizhonglm.diap.presenters.user;

import com.alibaba.fastjson.JSON;
import com.bruce.common.utils.EmptyUtil;
import com.bruce.common.utils.FileUtil;
import com.huizhonglm.diap.api.API;
import com.huizhonglm.diap.api.CommonResponse;
import com.huizhonglm.diap.api.HttpCallback;
import com.huizhonglm.diap.api.Status;
import com.huizhonglm.diap.api.model.user.PasswdResetParams;
import com.huizhonglm.diap.ui.user.iView.PwdResetIView;

/**
 * 描述当前类的作用
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2016-03-21 09:33
 */
public class PasswdResetPresenter {

    private PwdResetIView iView;

    public PasswdResetPresenter(PwdResetIView iView) {
        this.iView = iView;
    }
    
    public void resetPasswd(final String phoneNum, final String newPwd){
        PasswdResetParams params = new PasswdResetParams();
        params.account = phoneNum;
        params.passwd = FileUtil.MD5(newPwd);
        API.http().post("resetPasswd", params, new HttpCallback() {
            @Override
            public void onSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    CommonResponse res = JSON.parseObject(result, CommonResponse.class);
                    if (res != null && Status.SUCCESS.equals(res.getStatus())) {
                        iView.onResetSuccess();
                    } else {
                        iView.onResetFailure();
                    }
                } else {
                    iView.onResetFailure();
                }
            }

            @Override
            public void onFailure(Throwable ex) {
                iView.onResetFailure();
            }
        });
    }

}
